import { songUrl, Lyric } from "../../api/index";
import { Message } from "element-ui";



export const commonJs = {
  data() {
    return {
      availWidth: null,
      innerWidth: null,
    };
  },
  mounted() {
    this.innerWidth = window.innerWidth;
    this.availWidth = screen.availWidth;
    window.onresize = () => {
      return (() => {
        this.innerWidth = window.innerWidth;
        this.availWidth = screen.availWidth;
      })();
    };
  },
};

export const downFile = {
  methods: {
    dowmFile() {
      if (this.fileId == 1) {
        songUrl({ id: this.singInfo.id, br: 128000 }).then((res) => {
          this.centerDialogVisible = false;
          require("file-saver").saveAs(res.data[0].url, this.singInfo.name);
        });
      } else if (this.fileId == 2) {
        songUrl({ id: this.singInfo.id, br: 192000 }).then((res) => {
          this.centerDialogVisible = false;
          require("file-saver").saveAs(res.data[0].url, this.singInfo.name);
        });
      } else {
        songUrl({ id: this.singInfo.id, br: 999000 }).then((res) => {
          this.centerDialogVisible = false;
          require("file-saver").saveAs(res.data[0].url, this.singInfo.name);
        });
      }
    },
    downMP3(id, name) {
      this.domLoading2 = true;
      this.singInfo.id = id;
      this.singInfo.name = name;
      songUrl({ id, br: 128000 }).then((res) => {
        if (
          res.data[0].url == null ||
          res.data[0].url == undefined ||
          res.data[0].url == ""
        ) {
          this.toneQuality.splice(0, 1);
        }
        songUrl({ id, br: 192000 }).then((res) => {
          if (
            res.data[0].url == null ||
            res.data[0].url == undefined ||
            res.data[0].url == ""
          ) {
            this.toneQuality.splice(1, 1);
          }
          songUrl({ id, br: 999000 }).then((res) => {
            if (
              res.data[0].url == null ||
              res.data[0].url == undefined ||
              res.data[0].url == ""
            ) {
              this.toneQuality.splice(2, 1);
            }
            if (this.toneQuality.length == 0) {
              this.isSingDown = false;
            }
            this.domLoading2 = false;
            this.centerDialogVisible = true;
          });
        });
      });
    },
  },
};

export const boFangSing = {
  methods: {
    boFangSing(name, artists, picUrl, id) {
      songUrl({ id }).then((res) => {
        if (
          res.data[0].url == null ||
          res.data[0].url == undefined ||
          res.data[0].url == ""
        ) {
          Message({
            showClose: true,
            message: "暂无音源,无法播放",
            type: "error",
          });
        } else {
          Lyric({ id }).then((geci) => {
            this.$store.dispatch("boFangSing", {
              name,
              artists,
              picUrl,
              id,
              src: res.data[0].url,
              lrc: geci.lrc.lyric,
            });
            //刷新dom
            this.$store.state.musicInfo.isAutoplay = false;
            this.$store.state.musicInfo.showAplayer = false;
            this.$nextTick(() => {
              this.$store.state.musicInfo.isAutoplay = true;
              this.$store.state.musicInfo.showAplayer = true;
            });
          });
        }
      });
    },
  },
};

export const commonDongTaiComputed = {
  computed: {
    dongTaiStatus() {
      return function(type) {
        if (type == 31) {
          return "分享评论";
        } else if (type == 18) {
          return "分享单曲";
        } else if (type == 19) {
          return "分享专辑";
        } else if (type == 17 || type == 28) {
          return "分享博客";
        } else if (type == 22) {
          return "转发";
        } else if (type == 39) {
          return "发布视频";
        } else if (type == 13) {
          return "分享歌单";
        } else if (type == 24) {
          return "分享专栏文章";
        } else if (type == 56 || type == 57 || type == 35) {
          return "发布动态";
        } else if (type == 21) {
          return "分享MV";
        } else if (type == 41) {
          return "分享视频";
        } else {
          return "未知";
        }
      };
    },
  },
};
